package com.sports.demo.utils;

import org.apache.commons.lang3.ArrayUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * <p>
 *     字符串工具类
 * </p>
 *
 * @author liuning
 * @date 9:58 上午 2022/2/5
 */
public class StringUtils {

    public static final String EMPTY = "";

    public static final String REGEX_PHONE = "^1(3\\d{2}|4[14-9]\\d|5([0-35689]\\d|7[1-79])|66\\d|7[2-35-8]\\d|8\\d{2}|9[13589]\\d)\\d{7}$";

    public static final String REGEX_NUMBER = "[0-9]+";

    private static final String NUMBER = "0123456789";

    public static String uuid() {
        return UUID.randomUUID().toString();
    }

    public static String randomUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static boolean hasText(String value) {
        return org.springframework.util.StringUtils.hasText(value);
    }

    public static boolean isPhone(String phone) {
        if (!hasText(phone) || phone.length() != 11) {
            return false;
        }
        return Pattern.compile(REGEX_PHONE).matcher(phone).matches();
    }

    public static boolean isNumber(String phone) {
        if (!hasText(phone)) {
            return false;
        }
        return Pattern.compile(REGEX_NUMBER).matcher(phone).matches();
    }

    public static String numberUuid(int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(NUMBER.length());
            sb.append(NUMBER.charAt(number));
        }
        return sb.toString();
    }

    public static Double IntToDouble(Integer value,int value1,int length) {

        return new BigDecimal(value).divide(new BigDecimal(value1),length, RoundingMode.HALF_UP).doubleValue();
    }

    public static int[] toCodePoints(CharSequence str) {
        if (str == null) {
            return null;
        } else if (str.length() == 0) {
            return ArrayUtils.EMPTY_INT_ARRAY;
        } else {
            String s = str.toString();
            int[] result = new int[s.codePointCount(0, s.length())];
            int index = 0;

            for(int i = 0; i < result.length; ++i) {
                result[i] = s.codePointAt(index);
                index += Character.charCount(result[i]);
            }

            return result;
        }
    }
}
